package org.beetl.json;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.beetl.json.node.ClassNode;


/**
 * 序列化工具
 * @author xiandafu	
 *
 */
public class JsonTool {
	static Map<Class ,Policy> cache = new ConcurrentHashMap<Class ,Policy>  ();
	static List<Location> defaultLocs = new ArrayList<Location>();
	public static Policy getDefaultPolicy(Class c){	
	
		Policy p = cache.get(c);
		if(p==null){
			p = createPolicy(c);
			cache.put(c, p);
		}
		return p;
	}
	

	
	
	/**创建一个序列化类,使用类自己的序列化策略
	 * @param c
	 * @param policy
	 * @return
	 */
	private static Policy createPolicy(Class c){
		PolicyParser parser = new PolicyParser();
		Location location = parser.init(c);
		location.children.addAll(defaultLocs);
		ClassNode classNode = new ClassNode(c,location);
		classNode.parse();
		Policy jsonPolicy = new Policy(classNode);
		return jsonPolicy;
	}
	
	public  static void addLocationAction(String loc){
		List<Location>  list = PolicyParser.parse(loc);
		defaultLocs.addAll(list);
	}
	
	
	
}
